package com.lenovo.manufacture.module;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Module10 extends BaseActivity {

    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_content)
    ViewPager vpContent;

    List<View> views;
    List<Map<String, String>> infos;
    String[] title = {"推荐", "资讯", "热点", "口碑"};

    @Layout(value = R.layout.activity_module10, title = "汽车资讯")
    protected void init() {
        getSupportActionBar().setElevation(0);
        async(apiService.getAllInfo(), data -> {
            infos = data.getData();
            initViewPager();
        });
    }

    private void initViewPager() {
        views = new ArrayList<>();
        views.add(getView(0));
        views.add(getView(1));
        views.add(getView(2));
        views.add(getView(3));
        vpContent.setAdapter(new InfoPagerAdapter());
        tlTab.setupWithViewPager(vpContent);
    }

    private View getView(int i) {
        List<Map<String, String>> collect = infos.stream().filter(info -> Integer.valueOf(info.get("status")) == i).collect(Collectors.toList());
        RecyclerView recyclerView = new RecyclerView(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RvAdapter(collect));
        return recyclerView;
    }

    class RvAdapter extends RecyclerView.Adapter<ViewHolder> {
        List<Map<String, String>> infos;

        public RvAdapter(List<Map<String, String>> infos) {
            this.infos = infos;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_info, parent, false);
            ViewHolder viewHolder = new ViewHolder(inflate);
            inflate.setOnClickListener(v -> {
                Map<String, String> info = infos.get(viewHolder.getAdapterPosition());
                Intent intent = new Intent(parent.getContext(), Module10_1.class);
                intent.putExtra("name", info.get("informationName"));
                intent.putExtra("time", info.get("time"));
                intent.putExtra("word", info.get("words"));
                parent.getContext().startActivity(intent);
            });
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Map<String, String> info = infos.get(position);
            holder.textView.setText(info.get("informationName"));
            holder.textView2.setText(info.get("words"));
            holder.textView3.setText(simpleDateFormat.format(new Date(Long.valueOf(info.get("time")) * 1000)));
        }

        @Override
        public int getItemCount() {
            return infos.size();
        }

    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.textView)
        TextView textView;
        @BindView(R.id.textView2)
        TextView textView2;
        @BindView(R.id.textView3)
        TextView textView3;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    class InfoPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return object == view;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}